var table;
var layer;
var weid;
let name;
layui.use(['table', 'layer'], function () {
    table = layui.table;
    layer = layui.layer;
    $("#add").click(function () {
        layer.open({
            type: 2,
            title: "添加岩性描述",
            content: './AddDesc.html',
            area: ['500px', '400px'],
            btn: ['确认', '取消'],
            success: function (layero, index) {
                var iframe = window["layui-layer-iframe" + index]
                iframe.child(weid, name);
            },
            yes: function (index) {
                var res = window["layui-layer-iframe" + index].resdate();
                if (res == 200) {
                    layer.msg("添加完成", { icon: 1 });
                    setTimeout(function () {
                        window.location.reload(); //添加成功后刷新父界面
                    }, 1000);

                    layer.close(index)
                }
            }
        })
    })

})
function child(wellid, dbname) {
    weid = wellid;
    name = dbname;
    // //身份验证
    //   var getoken = GetToken(dataobj);
    table.set({
        headers: {
            Authorization: getoken
        }
    })
    table.render({
        elem: "#demo",
        type: 'get',
        url: api_address + "/gd/GetDescdate?wellid=" + weid + "&dbname=" + dbname,
        page: false,
        parseData: function (resdata) {
            var res = JSON.parse(resdata);
            return {
                "code": 0,
                "count": res.count,
                "msg": res.msg,
                "data": res.data
            }
        },
        cols: [[
            { field: 'id', type: 'numbers', title: 'ID', width: 100 }
            , { field: 'depth', title: '深度', width: 100 }
            , { field: 'desc', title: '描述' }
            , { title: '操作', width: 200, align: 'center', toolbar: '#roletool' }
        ]]
    })
    table.on('tool(test)', function (obj) {
        var data = obj.data //获得当前行数据				    
            ,
            layEvent = obj.event; //获得 lay-event 对应的值
        if (layEvent == 'del') {
            layer.confirm('确定要删除吗？', function (index) {
                $.ajax({
                    type: "post",
                    url: api_address + "/gd/Deletedescdate?wellid=" + weid + "&dbname=" + dbname + "&id=" + data.id,
                    contentType: 'application/json;',
                    headers: {
                        Authorization: getoken
                    },
                    success: function (data) {
                        var res = JSON.parse(data);
                        if (res.code == 200) {
                            //删除这一行，前端界面的修改，直接删除了这一条数据
                            obj.del();
                            //关闭弹框
                            layer.close(index);
                            layer.msg("删除成功", {
                                icon: 1
                            });
                        } else {
                            layer.msg("删除失败", {
                                icon: 2
                            });
                        }
                    }
                })
            })
        } else if (layEvent == 'edit') {
            layer.open({
                type: 2,
                title: "修改岩性描述",
                content: './EditDesc.html',
                area: ['500px', '400px'],
                btn: ['确认', '取消'],
                success: function (layero, index) {
                    var iframe = window["layui-layer-iframe" + index]
                    iframe.child(weid, data.id, data.depth, data.desc, dbname);
                },
                yes: function (index) {
                    var res = window["layui-layer-iframe" + index].resdate();
                    if (res == 200) {
                        layer.msg("修改完成", { icon: 1 });
                        setTimeout(function () {
                            window.location.reload(); //添加成功后刷新父界面
                        }, 1000);

                        layer.close(index)
                    }
                }

            })
        }
    })

}